//不足一分钟按一分钟,不足一小时显示分钟

function timeFormat (time) {
  var nowTime = new Date();
  var sendTime = new Date(time);
  var text;
  //不是本天
  if (sendTime.getTime() < countOneDayStart(nowTime.getTime())) {
    if (nowTime.getFullYear() == sendTime.getFullYear()) {
      text = (sendTime.getMonth() < 9 ? '0' + (sendTime.getMonth() + 1) : (sendTime.getMonth() + 1)) + '.' + (sendTime.getDate() < 10 ? '0' + sendTime.getDate() : sendTime.getDate());
    } else {
      text = sendTime.getFullYear() + '.' + (sendTime.getMonth() < 9 ? '0' + (sendTime.getMonth() + 1) : (sendTime.getMonth() + 1)) + '.' + (sendTime.getDate() < 10 ? '0' + sendTime.getDate() : sendTime.getDate());
    }
  } else {
    if (nowTime.getTime() - time < 60 * 1000) {
      text = '1分钟前';
    } else if (nowTime.getTime() - time < 3600 * 1000) {
      text = Math.floor((nowTime.getTime() - time) / 60 / 1000) + '分钟前';
    } else if (nowTime.getTime() - time < 3600 * 1000 * 24) {
      text = Math.floor((nowTime.getTime() - time) / 1000 / 3600) + '小时前';
    } else {
      if (nowTime.getFullYear() == sendTime.getFullYear()) {
        text = (sendTime.getMonth() < 9 ? '0' + (sendTime.getMonth() + 1) : (sendTime.getMonth() + 1)) + '.' + (sendTime.getDate() < 10 ? '0' + sendTime.getDate() : sendTime.getDate());
      } else {
        text = sendTime.getFullYear() + '.' + (sendTime.getMonth() < 9 ? '0' + (sendTime.getMonth() + 1) : (sendTime.getMonth() + 1)) + '.' + (sendTime.getDate() < 10 ? '0' + sendTime.getDate() : sendTime.getDate());
      }
    }
  }
  
  return text;
}

function timeFormat1 (time) {
  var nowTime = new Date();
  var sendTime = new Date(time);
  var text;
  //不是本天
  if (sendTime.getTime() < countOneDayStart(nowTime.getTime())) {
    if (nowTime.getFullYear() == sendTime.getFullYear()) {
      text = (sendTime.getMonth() < 9 ? '0' + (sendTime.getMonth() + 1) : (sendTime.getMonth() + 1)) + '.' + (sendTime.getDate() < 10 ? '0' + sendTime.getDate() : sendTime.getDate());
    } else {
      text = sendTime.getFullYear() + '.' + (sendTime.getMonth() < 9 ? '0' + (sendTime.getMonth() + 1) : (sendTime.getMonth() + 1)) + '.' + (sendTime.getDate() < 10 ? '0' + sendTime.getDate() : sendTime.getDate());
    }
  } else {
    if (nowTime.getTime() - time < 60 * 1000) {
      text = '1分钟前';
    } else if (nowTime.getTime() - time < 3600 * 1000) {
      text = Math.floor((nowTime.getTime() - time) / 60 / 1000) + '分钟前';
    } else if (nowTime.getTime() - time < 3600 * 1000 * 24) {
      text = (sendTime.getHours() < 10 ? '0' : '') + sendTime.getHours() + ':' + (sendTime.getMinutes() < 10 ? '0' : '') + sendTime.getMinutes();
    } else {
      if (nowTime.getFullYear() == sendTime.getFullYear()) {
        text = (sendTime.getMonth() < 9 ? '0' + (sendTime.getMonth() + 1) : (sendTime.getMonth() + 1)) + '.' + (sendTime.getDate() < 10 ? '0' + sendTime.getDate() : sendTime.getDate());
      } else {
        text = sendTime.getFullYear() + '.' + (sendTime.getMonth() < 9 ? '0' + (sendTime.getMonth() + 1) : (sendTime.getMonth() + 1)) + '.' + (sendTime.getDate() < 10 ? '0' + sendTime.getDate() : sendTime.getDate());
      }
    }
  }
  
  return text;
}

//指定时间过去了多少周
function timePositon (startTime, nowTime) {
  if (!nowTime) {
    nowTime = new Date().getTime();
  }
  var a = nowTime - startTime;
  return parseInt(a / (7 * 24 * 60 * 60 * 1000)) + 1;
}

//计算每学期开始日
function countSemesterStart (startDay, nowWeek) {
  var nowDate = new Date();
  nowDate.setHours(0, 0, 0, 0);
  var nowDay = nowDate.getDay() == 0 ? 7 : nowDate.getDay();
  var nowWeekStart;
  if (nowDay == startDay) {
    nowWeekStart = nowDate.getTime();
  } else if (nowDay > startDay) {
    nowWeekStart = nowDate.getTime() - (nowDay - startDay) * 24 * 3600 * 1000;
  } else if (nowDay < startDay) {
    nowWeekStart = nowDate.getTime() - (nowDay - startDay + 7) * 24 * 3600 * 1000;
  }
  return nowWeekStart - (nowWeek - 1) * 7 * 24 * 3600 * 1000;
}

//计算一天开始
function countOneDayStart (time) {
  var nowDate = time ? new Date(time) : new Date();
  nowDate.setHours(0, 0, 0, 0);
  return nowDate.getTime();
}

//获取当月第一天
function getFirstDayOfMonth (date) {
  var nowDate = time ? new Date() : new Date(date);
  nowDate.setHours(0, 0, 0, 0);
  nowDate.setDate(1);
  return nowDate;
}

//时间搓 转时间YYYY.MM.DD
function timetrans11 (date) {
  var date = new Date(date); //如果date为10位不需要乘1000
  var Y = date.getFullYear() + '.';
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '.';
  var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
  return Y + M + D;
}

//时间戳 YYYY-MM-DD mm:ss
function timetrans12 (timestamp) {
  var date = new Date(timestamp);
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  m = m < 10 ? ('0' + m) : m;
  var d = date.getDate();
  d = d < 10 ? ('0' + d) : d;
  var h = date.getHours();
  h = h < 10 ? ('0' + h) : h;
  var minute = date.getMinutes();
  var second = date.getSeconds();
  minute = minute < 10 ? ('0' + minute) : minute;
  second = second < 10 ? ('0' + second) : second;
  return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
}

//YYYY/MM/DD mm:ss:00转时间戳
function timetrans13 (time) {
  return new Date((time).replace(new RegExp('-', 'gm'), '/')).getTime();
}

//时间戳转化为2018.09.21 周五 14:24
function timetrans14 (timestamp) {
  var date = new Date(timestamp);
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  m = m < 10 ? ('0' + m) : m;
  var d = date.getDate();
  d = d < 10 ? ('0' + d) : d;
  var h = date.getHours();
  h = h < 10 ? ('0' + h) : h;
  var minute = date.getMinutes();
  var second = date.getSeconds();
  minute = minute < 10 ? ('0' + minute) : minute;
  second = second < 10 ? ('0' + second) : second;
  return y + '.' + m + '.' + d + ' ' + numberToWeekDay(date.getDay()) + ' ' + h + ':' + minute;
}

//时间戳 YYYY/MM/DD mm:ss
function timetrans15 (timestamp) {
  var date = new Date(timestamp);
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  m = m < 10 ? ('0' + m) : m;
  var d = date.getDate();
  d = d < 10 ? ('0' + d) : d;
  var h = date.getHours();
  h = h < 10 ? ('0' + h) : h;
  var minute = date.getMinutes();
  var second = date.getSeconds();
  minute = minute < 10 ? ('0' + minute) : minute;
  second = second < 10 ? ('0' + second) : second;
  return y + '/' + m + '/' + d + ' ' + h + ':' + minute;
}

//时间戳 YYYY-MM/-DD mm:ss
function timetrans25 (timestamp) {
  var date = new Date(timestamp);
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  m = m < 10 ? ('0' + m) : m;
  var d = date.getDate();
  d = d < 10 ? ('0' + d) : d;
  var h = date.getHours();
  h = h < 10 ? ('0' + h) : h;
  var minute = date.getMinutes();
  var second = date.getSeconds();
  minute = minute < 10 ? ('0' + minute) : minute;
  second = second < 10 ? ('0' + second) : second;
  return y + '-' + m + '-' + d + ' ' + h + ':' + minute;
}

function timetrans16 (timestamp) {
  var date = new Date(timestamp);
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  var d = date.getDate();
  var h = date.getHours();
  var minute = date.getMinutes();
  var second = date.getSeconds();
  return [
    y,
    m,
    d,
    h,
    minute,
    second];
}

//时间戳转化为2018.09.21 Fri 14:24
function timetrans17 (timestamp) {
  var date = new Date(timestamp);
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  m = m < 10 ? ('0' + m) : m;
  var d = date.getDate();
  d = d < 10 ? ('0' + d) : d;
  var h = date.getHours();
  h = h < 10 ? ('0' + h) : h;
  var minute = date.getMinutes();
  var second = date.getSeconds();
  minute = minute < 10 ? ('0' + minute) : minute;
  second = second < 10 ? ('0' + second) : second;
  return y + '.' + m + '.' + d + ' ' + getDay1(date.getDay()) + ' ' + h + ':' + minute;
}

//YYYY-MM-DD hh:mm:ss转时间戳
function timetrans18 (date) {
  var arr1 = date.split(' ')[0].split('-');
  var arr2 = date.split(' ')[1].split(':');
  var newDate = new Date();
  newDate.setFullYear(arr1[0] || 1970, arr1[1] - 1, arr1[2] || 1);
  newDate.setHours(arr2[0] || 0, arr2[1] || 0, arr2[2] || 0);
  return newDate.getTime();
}

//时间戳 转时间YYYY-MM-DD
function timetrans19 (date) {
  var date = new Date(date); //如果date为10位不需要乘1000
  var Y = date.getFullYear() + '-';
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
  return Y + M + D;
}

//判断是星期几
function numberToWeekDay (type) {
  var text = '';
  switch (Number(type)) {
    case 1:
      text = '星期一';
      break;
    case 2:
      text = '星期二';
      break;
    case 3:
      text = '星期三';
      break;
    case 4:
      text = '星期四';
      break;
    case 5:
      text = '星期五';
      break;
    case 6:
      text = '星期六';
      break;
    case 7:
    case 0:
      text = '星期天';
      break;
  }
  return text;
}

//获取时间戳日期英文
function getDay1 (timestamp) {
  var day = new Date(timestamp).getDay();
  var text;
  switch (day) {
    case 0:
      text = 'Sunday';
      break;
    case 1:
      text = 'Monday';
      break;
    case 2:
      text = 'Tuesday';
      break;
    case 3:
      text = 'Wednesday';
      break;
    case 4:
      text = 'Thursday';
      break;
    case 5:
      text = 'Friday';
      break;
    case 6:
      text = 'Saturday';
      break;
  }
  return text;
}
